###Space, Time, Space vs. Time#####
###02/01/2022######################
###Generate Tables##################


setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#Get packages, attach them
pks=c("reshape2", "xtable")
needed=setdiff(pks,installed.packages())

if( length(needed) > 0){
  options(Ncpus=parallel::detectCores() %/% 1.25)
  install.packages(needed)
}

sapply(pks, library, character.only=T)
session = sessionInfo()



options(scipen=999)


load("FINALphi_0.RData")

beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 
phi.results.ldv[5,] <- phi.results.ldv[5,]/tr
phi.results.stadl[5,] <- phi.results.stadl[5,]/tr
rho.results.sar[5,] <- rho.results.sar[5,]/tr
rho.results.stadl[5,] <- rho.results.stadl[5,]/tr

phi_0_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1],phi.results.ldv[,1],phi.results.stadl[,1],rho.results.sar[,1],rho.results.stadl[,1]),
cbind(beta.results.static[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3],phi.results.ldv[,3],phi.results.stadl[,3],rho.results.sar[,3],rho.results.stadl[,3]),
cbind(beta.results.static[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5],phi.results.ldv[,5],phi.results.stadl[,5],rho.results.sar[,5],rho.results.stadl[,5]),
cbind(beta.results.static[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7],phi.results.ldv[,7],phi.results.stadl[,7],rho.results.sar[,7],rho.results.stadl[,7])))

colnames(phi_0_Table) <- c("Static","LDV","SAR","STADL","LDV","STADL","SAR","STADL")
rownames(phi_0_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


print(xtable(phi_0_Table, type = "latex", digits=3), file = "phi_0_Table.tex")


load("FINALphi_0.1.RData")


beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 
phi.results.ldv[5,] <- phi.results.ldv[5,]/tr
phi.results.stadl[5,] <- phi.results.stadl[5,]/tr
rho.results.sar[5,] <- rho.results.sar[5,]/tr
rho.results.stadl[5,] <- rho.results.stadl[5,]/tr

phi_01_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1],phi.results.ldv[,1],phi.results.stadl[,1],rho.results.sar[,1],rho.results.stadl[,1]),
                               cbind(beta.results.static[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3],phi.results.ldv[,3],phi.results.stadl[,3],rho.results.sar[,3],rho.results.stadl[,3]),
                               cbind(beta.results.static[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5],phi.results.ldv[,5],phi.results.stadl[,5],rho.results.sar[,5],rho.results.stadl[,5]),
                               cbind(beta.results.static[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7],phi.results.ldv[,7],phi.results.stadl[,7],rho.results.sar[,7],rho.results.stadl[,7])))

colnames(phi_01_Table) <- c("Static","LDV","SAR","STADL","LDV","STADL","SAR","STADL")
rownames(phi_01_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


#print(xtable(phi_01_Table, type = "latex", digits=3), file = "phi_01_Table.tex")


load("FINALphi_0.2.RData")

beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 
phi.results.ldv[5,] <- phi.results.ldv[5,]/tr
phi.results.stadl[5,] <- phi.results.stadl[5,]/tr
rho.results.sar[5,] <- rho.results.sar[5,]/tr
rho.results.stadl[5,] <- rho.results.stadl[5,]/tr

phi_02_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1],phi.results.ldv[,1],phi.results.stadl[,1],rho.results.sar[,1],rho.results.stadl[,1]),
                               cbind(beta.results.static[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3],phi.results.ldv[,3],phi.results.stadl[,3],rho.results.sar[,3],rho.results.stadl[,3]),
                               cbind(beta.results.static[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5],phi.results.ldv[,5],phi.results.stadl[,5],rho.results.sar[,5],rho.results.stadl[,5]),
                               cbind(beta.results.static[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7],phi.results.ldv[,7],phi.results.stadl[,7],rho.results.sar[,7],rho.results.stadl[,7])))

colnames(phi_02_Table) <- c("Static","LDV","SAR","STADL","LDV","STADL","SAR","STADL")
rownames(phi_02_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


#print(xtable(phi_02_Table, type = "latex", digits=3), file = "phi_02_Table.tex")


load("FINALphi_0.3.RData")

beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 
phi.results.ldv[5,] <- phi.results.ldv[5,]/tr
phi.results.stadl[5,] <- phi.results.stadl[5,]/tr
rho.results.sar[5,] <- rho.results.sar[5,]/tr
rho.results.stadl[5,] <- rho.results.stadl[5,]/tr

phi_03_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1],phi.results.ldv[,1],phi.results.stadl[,1],rho.results.sar[,1],rho.results.stadl[,1]),
                               cbind(beta.results.static[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3],phi.results.ldv[,3],phi.results.stadl[,3],rho.results.sar[,3],rho.results.stadl[,3]),
                               cbind(beta.results.static[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5],phi.results.ldv[,5],phi.results.stadl[,5],rho.results.sar[,5],rho.results.stadl[,5]),
                               cbind(beta.results.static[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7],phi.results.ldv[,7],phi.results.stadl[,7],rho.results.sar[,7],rho.results.stadl[,7])))

colnames(phi_03_Table) <- c("Static","LDV","SAR","STADL","LDV","STADL","SAR","STADL")
rownames(phi_03_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


#print(xtable(phi_03_Table, type = "latex", digits=3), file = "phi_03_Table.tex")


load("FINALphi_0.4.RData")


beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 
phi.results.ldv[5,] <- phi.results.ldv[5,]/tr
phi.results.stadl[5,] <- phi.results.stadl[5,]/tr
rho.results.sar[5,] <- rho.results.sar[5,]/tr
rho.results.stadl[5,] <- rho.results.stadl[5,]/tr

phi_04_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1],phi.results.ldv[,1],phi.results.stadl[,1],rho.results.sar[,1],rho.results.stadl[,1]),
                               cbind(beta.results.static[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3],phi.results.ldv[,3],phi.results.stadl[,3],rho.results.sar[,3],rho.results.stadl[,3]),
                               cbind(beta.results.static[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5],phi.results.ldv[,5],phi.results.stadl[,5],rho.results.sar[,5],rho.results.stadl[,5]),
                               cbind(beta.results.static[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7],phi.results.ldv[,7],phi.results.stadl[,7],rho.results.sar[,7],rho.results.stadl[,7])))

colnames(phi_04_Table) <- c("Static","LDV","SAR","STADL","LDV","STADL","SAR","STADL")
rownames(phi_04_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


#print(xtable(phi_04_Table, type = "latex", digits=3), file = "phi_04_Table.tex")


load("FINALphi_0.5.RData")


beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 
phi.results.ldv[5,] <- phi.results.ldv[5,]/tr
phi.results.stadl[5,] <- phi.results.stadl[5,]/tr
rho.results.sar[5,] <- rho.results.sar[5,]/tr
rho.results.stadl[5,] <- rho.results.stadl[5,]/tr

phi_05_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1],phi.results.ldv[,1],phi.results.stadl[,1],rho.results.sar[,1],rho.results.stadl[,1]),
                               cbind(beta.results.static[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3],phi.results.ldv[,3],phi.results.stadl[,3],rho.results.sar[,3],rho.results.stadl[,3]),
                               cbind(beta.results.static[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5],phi.results.ldv[,5],phi.results.stadl[,5],rho.results.sar[,5],rho.results.stadl[,5]),
                               cbind(beta.results.static[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7],phi.results.ldv[,7],phi.results.stadl[,7],rho.results.sar[,7],rho.results.stadl[,7])))

colnames(phi_05_Table) <- c("Static","LDV","SAR","STADL","LDV","STADL","SAR","STADL")
rownames(phi_05_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


print(xtable(phi_05_Table, type = "latex", digits=3), file = "phi_0.5_Table.tex")

